package j_util;

import gui.other.CronometerAction;

import javax.swing.*;
import java.awt.*;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Date: Oct 2, 2009
 * Time: 7:44:22 AM
 * @author Jose Martin Rozanec;
 */
public class Cronometer extends Thread {
    private int hours;
    private int minutes;
    private int seconds;
    private JLabel label;
    private String labelText;
    private Component container;
    private boolean showTimeOnLabel;
    private CronometerAction action;
    private boolean hasTimedOut;
    private boolean reseted;

    public void setRegressionTime(int minutes) {
        this.hours = minutes / 60;
        this.minutes = minutes - hours * 60;
    }

    public void run() {
        hasTimedOut = false;
        reseted = false;
        boolean terminate=false;
        do {
            if (seconds <= 0) {
                if (minutes > 0) {
                    seconds = 59;
                    minutes--;
                } else {
                    if (hours > 0) {
                        hours--;
                        minutes = 59;
                        seconds = 59;
                    }
                }
                if (hours == 0 && minutes == 0 && seconds == 0) {
                    terminate=true;//condicion de interrupcion
                }
            } else {
                seconds--;
            }

            manageLabelsText();
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        while (hours >= 0 && minutes >= 0 && !terminate);
        hasTimedOut = true;
        if (!reseted) {
            performActionOnTimeout();
        }
    }

    public int getMinutes() {
        return minutes;
    }

    public void setLabel(JLabel label) {
        this.label = label;
        label.revalidate();
    }

    public void setLabelText(String labelText) {
        this.labelText = labelText;
    }

    public void setContainer(Component container) {
        this.container = container;
    }

    public void reset() {
        hours = 0;
        minutes = 0;
        seconds = 0;
        setLabelText(labelText);
        reseted = true;
    }

    private String getFormattedString() {
        StringBuffer buffer = new StringBuffer();
        formattedStringAux(buffer, hours, ":");
        formattedStringAux(buffer, minutes, ":");
        formattedStringAux(buffer, seconds, "");
        return buffer.toString();
    }

    private void formattedStringAux(StringBuffer buffer, int number, String endsWith) {
        if (number < 10) {
            buffer.append("0" + number);
        } else {
            buffer.append(number);
        }
        buffer.append(endsWith);
    }

    /**
     * Permite establecer si se muestra el tiempo de descuento en el label o no;
     */
    public void showTimeOnLabel(boolean show) {
        this.showTimeOnLabel = show;
        manageLabelsText();
    }

    private void manageLabelsText() {
        if (label != null && container != null) {
            if (labelText != null) {
                if (showTimeOnLabel) {
                    label.setText(labelText + getFormattedString());
                } else {
                    label.setText(labelText + " --:---:--");
                }
            } else {
                if (showTimeOnLabel) {
                    label.setText(getFormattedString());
                } else {
                    label.setText(" --:---:--");
                }
            }
            label.validate();
//                container.revalidate();
            container.repaint();
            container.validate();
        }
    }

    public void setActionOnTimeout(CronometerAction action) {
        this.action = action;
    }

    public boolean hasTimedOut() {
        return hasTimedOut;
    }

    private void performActionOnTimeout() {
        this.action.execute();
    }
}
